<?php
/*
 * Created on Jan 27, 2008
 *
 * This is an open source software, please obey the GPL
 * The Referee Service is offered by Cacomand Inc. 
 * View Http://www.cacomand.com for support or more information
 * You can also view distrema.googlecode.com to join the community.
 */


/*
 * This file is to configure the link to the CCR database and
 * data-getting functions for the ccr.
 * This file also include a link to the ref
 */
include("open_ref_link.php"); 
 
/**
* The default IP address which is stastic.
* If users are using the epsns below, they can save the time of waiting for the result of the IP address of their CCR(~Oops..such a looooooooooong sentence)
*
* @var       string
* @access    public
*/
$default_ccr_epsn="bj_of_0001";//the default epsn

/**
* The default EPSN which is stastic.
* If users are using the epsns below, they can save the time of waiting for the result of the IP address of their CCR(~Oops..such a looooooooooong sentence)
*
* @var       string
* @access    public
*/
$default_ccr_host="127.0.0.1";//local
//$default_ccr_host="59.39.69.220";//Zhejiang ewaydns

/**
* Combine the epsn with IP address. Append to the list if you know more.
* CAUTION: The IP address writen here has more priority then the ones stored 
* 	in the REF server. that means if the IP address you definded here is wrong, 
*	the program will not be able to find out the right one.
*/
$ccr_hosts[$default_ccr_epsn]=$default_ccr_host;


/**
* The database name of the CCR server.
*
* @var       string
* @access    public
*/
$ccr_basename="sq_userbase";

/**
* The username with the full access of the CCR
*
* @var       string
* @access    public
*/
$ccr_public_username="sq_userbase";

/**
* The password with the full access of the CCR
*
* @var       string
* @access    public
*/
$ccr_public_key="MMNG4USXJdYPTL4L";

/**
* Determing if you want to have a runtime report for your development.
* CAUTION: Mostly,cookies will not be able to set if you tune on this option
*
* @var       boolean
* @access    public
*/
$developer=false;

/******************************************************************************
  * Give a DistRema licence.
  *
  * Returns formated licence list on success, false on error.
  *
  * usage: show_licence()
  *
  * @access    public
  * @return    string
  */
function show_licence()
{
	$item[]="all service should use the official REF server";
	$item[]="No limited content";
	$licence=implode("<br />",$item);
	return $licence;
}

/******************************************************************************
  * Get the username form the ccr by epsn and user ID
  *
  * Returns username on success, false on error.
  *
  * usage: get_username(string $epsn,string $user_ID)
  *
  * @access    public
  * @return    string
  */
function get_username($epsn,$user_ID)
{
	global $ccr_public_username,$ccr_public_key,$ccr_basename;
	global $developer;
	if(isset($ccr_link))
		mysql_close($ccr_link);//close the previous ccr link which may not be link to the same ccr
	$ccr_link=mysql_connect(get_ccr_host($epsn),$ccr_public_username,$ccr_public_key);
	mysql_select_db($ccr_basename,$ccr_link);//this link must be self
	if($ccr_link!=FALSE)
	{
		$select_format='SELECT username FROM user_list WHERE user_ID=%d';
		$query=sprintf($select_format,$user_ID);
		if($developer)
			echo("get_username:".$query."<br />");
		$res_username=mysql_query($query,$ccr_link);
		if($obj_username=mysql_fetch_object($res_username))
		{
			return $obj_username->username;
		}
		else
		{
			return FALSE;
		}
	}
}

/******************************************************************************
  * Get the IP address of the CCR identified by the epsn either in the configure
  * or from the REF database
  *
  * Returns IP address on success, false on error.
  *
  * usage: get_ccr_host(string $epsn)
  *
  * @access    public
  * @return    string
  */
function get_ccr_host($epsn)
{
	//global $ref_host,m$ref_basename,$ref_username,$ref_password;
	global $ref_link;
	global $developer;
	global $ccr_hosts;
	if(isset($ccr_hosts[$epsn]))
	{
		if($developer)
			echo("get_ccr_host:".$ccr_hosts[$epsn]."(from configure)<br />");
		return $ccr_hosts[$epsn];
	}
	else
	{
		/**$ref_link=mysql_connect($ref_host,$ref_username,$ref_password);
	mysql_select_db($ref_basename,$ref_link);*/	
		if ($ref_link!=FALSE)
		{
			$select_format='SELECT IP_address FROM epsn_pool WHERE epsn="%s"';
			$query=sprintf($select_format,$epsn);
			if($developer)
				echo("get_ccr_host:".$query."(from REF)<br />");//to find out the IP address for this epsn
			$res_ccr_host=mysql_query($query,$ref_link);
			if ($obj_ccr_host=mysql_fetch_object($res_ccr_host));
			{
				$got_host=$obj_ccr_host->IP_address;
				$isset_ccr_host=TRUE;
				if($developer)
					echo("get_ccr_host:CCR IP address found at ".$got_host."<br />");
			}
			//mysql_close($ref_link);//close the link to the Referee Server
			$ccr_hosts[$epsn]=$got_host;
			return $got_host;
		}
		else
			return false;
	}
}

/******************************************************************************
  * Get the user level of the user.
  * the higher the level is, the more reliable the user is.
  *
  * Returns user level on success, false on error, 0 on not exist.
  *
  * usage: get_user_level(string $epsn,integer $user_ID)
  *
  * @access    public
  * @return    integer
  */
function get_user_level($epsn,$user_ID)
{
	global $ref_link;
	global $developer;
	if ($ref_link!=FALSE)
	{
		$select_format='SELECT level FROM certification WHERE epsn="%s" AND user_ID=%d';
		$query=sprintf($select_format,$epsn,$user_ID);
		if($developer)
			echo("get_user_level:".$query."<br />");//to find out the IP address for this epsn
		$res_level=mysql_query($query,$ref_link);
		if ($res_level!=false)
		{
			$obj_level=mysql_fetch_object($res_level);
			$got_level=$obj_level->level;
			$isset_ccr_host=TRUE;
			if($developer)
				echo("get_user_level:user level got as ".$got_level."<br />");
		}
		else
		{
			$got_level=0;
		}
		return $got_level;
	}
	else
		return false;
}

/**
 * The user class offers the basic actions of a user. It's mainly about getting
 * data from the ccr directly. If you want to get data more smartly on a DPS, you
 * can use the child class-dps_user
 *
 * @version		0.5.35
 * @author		Arthur<arthur@cacomand.com>
 * @package		DPS
 *
 */
class user
{
	/**
	* The epsn of the user.
	* 
	* @var       string
	* @access    public
	*/
	public $epsn;
	
	/**
	* The user ID of the user.
	* 
	* @var       string
	* @access    public
	*/
	public $user_ID;
	
	/**
	* The username for login of the user.It's NOT SET UP AUTOMATICALLY, if you need this value, use $this->gain_username() first
	* 
	* @var       string
	* @access    public
	* @see       gain_username
	*/
	public $username;
	
	/**
	* The user level of the user.It's NOT SET UP AUTOMATICALLY, if you need this value, use $this->gain_user_level() first
	* 
	* @var       integer
	* @access    public
	* @see       gain_user_level
	*/
	public $user_level;
	
	/**
	* The IP address of the ccr of the user,It's set up when creating an object of this class.
	* 
	* @var       string
	* @access    public
	*/
	public $ccr_host;
	
	/******************************************************************************
	* Class constructor. Must be called with two parameters.
	* The first parameter sets the epsn of the user while the second parameter
	* sets the user ID. When constructing the class, the function will try to 
	* get the ccr host and set up a link to the ccr(in variable $this->ccr_link)
	*
	* Returns true on success, false on error
	*
	* usage: user(string $epsn,integer $user_ID)
	*
	* @param     $epsn        the epsn of the user
	* @param     $string      the user ID of the user
	* @see       get_ccr_host
	* @access    public
	* @return    boolean
	*/
	function user($epsn,$user_ID)
	{
		global $ccr_public_username,$ccr_public_key,$ccr_basename;
		global $developer;
		$this->epsn=$epsn;
		$this->user_ID=$user_ID;
		$this->ccr_host=get_ccr_host($epsn);//TC+1
		
		if(!empty($this->ccr_host))
		$isset_ccr_host=TRUE;
		else
		$isset_ccr_host=FALSE;//to record if the ip address of the CCR is found
		$isset_userinfo=FALSE;//to record if the userinfo is got successfully
		//if got the ccr_host successfully, link to the user's CCR, to thanslate his user name into userID, and get his personal infomation
		if ($isset_ccr_host)
		{
			$this->ccr_link=mysql_connect($this->ccr_host,$ccr_public_username,$ccr_public_key);
			mysql_select_db($ccr_basename,$this->ccr_link);
			if($this->ccr_link!=FALSE)
				$isset_userinfo=TRUE;
			else
				return false;
			//check if the generation is succeeful.
			if ($isset_userinfo and $isset_ccr_host)
				return true;
			else
				return false;
		}
	}
	
	/******************************************************************************
	* link to the ccr database and gain the username
	* because this variable is not necessary in all services,
	* so we set it alone in this function
	*
	* Returns username on success, false on error
	*
	* usage: gain_username()
	*
	* @access    public
	* @return    string
	*/
	function gain_username()
	{
		global $ccr_public_username,$ccr_public_key,$ccr_basename;
		global $developer;
		if($this->ccr_link!=FALSE)
		{
			$select_format='SELECT username FROM user_list WHERE user_ID=%d';
			$query=sprintf($select_format,$user_ID);
			if($developer)
				echo("gain_username:".$query."<br />");
			$res_username=mysql_query($query,$this->ccr_link);
			if($res_username!=false)
			{
				$obj_username=mysql_fetch_object($res_username);
				$this->username=$obj_username->username;
				return $obj_username->username;
			}
			else
			{
				return FALSE;
			}
		}
	}
				
	/******************************************************************************
	* link to the REF database and gain the user level
	* because this variable is not necessary in all services,
	* so we set it alone in this function
	*
	* Returns user level on success, false on error
	*
	* usage: gain_user_level()
	*
	* @access    public
	* @return    integer
	*/
	function gain_user_level()
	{
		$this->user_level=get_user_level($epsn,$user_ID);
	}
	
	/******************************************************************************
	* Get the variable identified by var_ID from the ccr user's own table.
	* You can use both caption and memo or one of them as extra condition(s).
	*
	* Returns an array contains the data on success,false on fail or error
	*
	* usage: get_var_from_ccr(integer $var_ID[,string $caption[,string $memo]])
	*
	* @access    public
	* @return    array[]["caption"|"Memo"]
	* @see		smart_get
	* @see		get_var_from_upload_cache
	* @see		get_var_from_dps
	* @see		get_var_from_sent_data
	*/
	function get_var_from_ccr($var_ID)
	{
		//global $ccr_public_username,$ccr_public_key,$ccr_basename;
		global $developer;
		if(func_num_args()==3)
		{
		$caption=func_get_arg(1);
		$memo=func_get_arg(2);
		}
		elseif(func_num_args()==2)
		{
		$caption=func_get_arg(1);
		$memo="";
		}
		else
		{
			$caption="";
			$memo="";
		}
		/**$ccr_link=mysql_connect($this->ccr_host,$ccr_public_username,$ccr_public_key);
		mysql_select_db($ccr_basename,$ccr_link);*/
		if($this->ccr_link!=FALSE)
		{
			if(empty($caption) AND empty($memo))//both caption and memo are empty, mean on extra condition
			{
				$select_format='SELECT caption,memo FROM variables_%d WHERE var_ID=%d';
				$query=sprintf($select_format,$this->user_ID,$var_ID);
			}
			elseif(empty($memo))//only caption is not empty, means use caption as extra condition
			{
				$select_format='SELECT caption,memo FROM variables_%d WHERE var_ID=%d AND caption LIKE "%s"';
				$query=sprintf($select_format,$this->user_ID,$var_ID,$caption."%");
			}
			elseif(empty($caption))//only memo is not empty, means use memo as extra condition
			{
				$select_format='SELECT caption,memo FROM variables_%d WHERE var_ID=%d AND memo LIKE "%s"';
				$query=sprintf($select_format,$this->user_ID,$var_ID,$memo."%");
			}
			else//both are not empty, means use both as extra condition. usually it's to check whether the record exist
			{
				$select_format='SELECT caption,memo FROM variables_%d WHERE var_ID=%d AND caption LIKE "%s" AND memo LIKE "%s"';
				$query=sprintf($select_format,$this->user_ID,$var_ID,$caption."%",$memo."%");
			}
			if($developer)
				echo("get_var_from_ccr:".$query."<br />");
			$res_var=mysql_query($query,$this->ccr_link);
			$m=0;
			while($obj_var=mysql_fetch_object($res_var))
			{
				$return_array[$m]["caption"]=$obj_var->caption;
				$return_array[$m]["memo"]=$obj_var->memo;
				$m++;
			}
			if($m!=0)
			{
				if($developer)
					echo("get_var_from_ccr:user ".$this->username."'s data (var_ID=".$var_ID.", caption=".$caption.") is acknowleged, and there's ".$m."records in total<br />");
				return $return_array;
			}
			else
			{
				if($developer)
					echo("get_var_from_ccr:user ".$this->username."'s data for this var type is not exist<br />");
				return false;
			}
		}
	}	
	
	/******************************************************************************
	* Get the variable identified by var_ID from the ccr user's sent data pool
	* You can use both caption and memo or one of them as extra condition(s).
	*
	* Returns an array contains the data on success,false on fail or error
	*
	* usage: get_var_from_ccr(integer $var_ID[,string $caption[,string $memo]])
	*
	* @see		smart_get
	* @see		get_var_from_upload_cache
	* @see		get_var_from_dps
	* @see		get_var_from_ccr
	* @access    public
	* @return    array[]["caption"|"Memo"]
	*/
	function get_var_from_sent_data($var_ID)
	{
		//global $ccr_public_username,$ccr_public_key,$ccr_basename;
		global $developer;
		if(func_num_args()==3)
		{
		$caption=func_get_arg(1);
		$memo=func_get_arg(2);
		}
		elseif(func_num_args()==2)
		{
		$caption=func_get_arg(1);
		$memo="";
		}
		else
		{
			$caption="";
			$memo="";
		}
		/**$ccr_link=mysql_connect($this->ccr_host,$ccr_public_username,$ccr_public_key);
		mysql_select_db($ccr_basename,$ccr_link);*/
		if($this->ccr_link!=FALSE)
		{
			if(empty($caption) AND empty($memo))//both caption and memo are empty, mean on extra condition
			{
				$select_format='SELECT caption,memo FROM sent_data_pool WHERE user_ID=%d AND var_ID=%d';
				$query=sprintf($select_format,$this->user_ID,$var_ID);
			}
			elseif(empty($memo))//only caption is not empty, means use caption as extra condition
			{
				$select_format='SELECT caption,memo FROM sent_data_pool WHERE user_ID=%d AND var_ID=%d AND caption LIKE "%s"';
				$query=sprintf($select_format,$this->user_ID,$var_ID,$caption."%");
			}
			elseif(empty($caption))//only memo is not empty, means use memo as extra condition
			{
				$select_format='SELECT caption,memo FROM sent_data_pool WHERE user_ID=%d AND var_ID=%d AND memo LIKE "%s"';
				$query=sprintf($select_format,$this->user_ID,$var_ID,$memo."%");
			}
			else//both are not empty, means use both as extra condition. usually it's to check whether the record exist
			{
				$select_format='SELECT caption,memo FROM sent_data_pool WHERE user_ID=%d AND var_ID=%d AND caption LIKE "%s" AND memo LIKE "%s"';
				$query=sprintf($select_format,$this->user_ID,$var_ID,$caption."%",$memo."%");
			}
			if($developer)
				echo("get_var_from_sent_data:".$query."<br />");
			$res_var=mysql_query($query,$this->ccr_link);
			//mysql_close($ccr_link);//close the link here,for the function may escape during the return
			$m=0;
			while($obj_var=mysql_fetch_object($res_var))
			{
				$return_array[$m]["caption"]=$obj_var->caption;
				$return_array[$m]["memo"]=$obj_var->memo;
				$m++;
			}
			if($m!=0)
			{
				if($developer)
					echo("get_var_from_sent_data:user ".$this->username."'s data (var_ID=".$var_ID.", caption=".$caption.") is aknowleged, and there's ".$m."records in total<br />");
				return $return_array;
			}
			else
			{
				if($developer)
					echo("get_var_from_sent_data:user ".$this->username."'s data for this var type is not exist<br />");
				return false;
			}
		}
	}
}
?>
